#include "myapp.h"
#include "it_dialog.h"

DECLARE_APP(MyApp);

BEGIN_EVENT_TABLE(ItemsDialog, wxDialog)
	EVT_BUTTON(items_ok,ItemsDialog::id_OkBtn_clicked)
	EVT_BUTTON(items_can,ItemsDialog::id_CanBtn_clicked)
	EVT_BUTTON(items_add,ItemsDialog::id_AddItems_clicked)
	EVT_BUTTON(items_mod,ItemsDialog::id_ModItems_clicked)
	EVT_BUTTON(items_del,ItemsDialog::id_DelItems_clicked)
	EVT_LISTBOX(items_list,ItemsDialog::id_ItemsList_selected)
END_EVENT_TABLE() 

ItemsDialog::ItemsDialog(wxWindow *parent)
:wxDialog(parent, wxID_ANY, _("Setting of Items"), wxDefaultPosition, wxDefaultSize)
{
#ifdef __WXMSW__
	SetClientSize(wxSize(400,230));
#else
	SetClientSize(wxSize(400,250));
#endif


	id_ItemsList = new wxListBox(this,items_list,wxDefaultPosition,wxSize(160,180),0,NULL);
	id_AddItems = new wxButton(this, items_add, _("Add"), wxPoint(-1,-1),wxSize(120,-1));
	id_ModItems = new wxButton(this, items_mod, _("Modify"), wxPoint(-1,-1),wxSize(120,-1));
	id_DelItems = new wxButton(this, items_del, _("Remove"), wxPoint(-1,-1),wxSize(120,-1));
	
	id_ItemsInfo = new wxStaticBox(this, wxID_ANY, _("Infomation of expense"));
	wxStaticText *label1 = new wxStaticText(this,wxID_ANY,_("Items name:"));
	id_InputItemsName = new wxTextCtrl(this, wxID_ANY, _T(""),wxDefaultPosition,wxSize(120,-1));
	
	id_ItemsImage = new wxBitmapComboBox(this,wxID_ANY,_T(""),wxDefaultPosition,wxSize(180,-1),0,NULL,wxCB_READONLY);

	id_OkBtn = new wxButton(this, items_ok, _T("OK"), wxPoint(-1,-1),wxSize(85,-1));
	id_CanBtn = new wxButton(this, items_can, _("Cancel"), wxPoint(-1,-1),wxSize(85,-1));
	
	wxBoxSizer *bsizer1 = new wxBoxSizer(wxVERTICAL);
		wxBoxSizer *bsizer2 = new wxBoxSizer(wxHORIZONTAL);
		bsizer2->Add(id_ItemsList,0,wxTOP|wxLEFT,0);
			wxBoxSizer *bsizer3 = new wxBoxSizer(wxVERTICAL);
				wxStaticBoxSizer *sbsizer = new wxStaticBoxSizer(id_ItemsInfo,wxVERTICAL);
				sbsizer->Add(id_AddItems,0,wxTOP|wxLEFT,0);
				sbsizer->Add(id_ModItems,0,wxTOP|wxLEFT,0);
				sbsizer->Add(id_DelItems,0,wxTOP|wxLEFT,0);
				sbsizer->Add(label1,0,wxTOP,15);
				sbsizer->Add(id_InputItemsName,0,wxTOP|wxLEFT,0);
				sbsizer->Add(id_ItemsImage,0,wxTOP,5);
			bsizer3->Add(sbsizer,1,wxTOP|wxLEFT|wxEXPAND,0);
		bsizer2->Add(bsizer3,0,wxTOP|wxLEFT,5);
	bsizer1->Add(bsizer2,0,wxBOTTOM,5);
		wxBoxSizer *bsizer4 = new wxBoxSizer(wxHORIZONTAL);
		bsizer4->Add(id_OkBtn,0,wxTOP|wxLEFT,0);
		bsizer4->Add(id_CanBtn,0,wxLEFT,5);
	bsizer1->Add(bsizer4,0,wxBOTTOM|wxRIGHT|wxALIGN_RIGHT,10);
	this->SetSizer(bsizer1);
	this->Layout();
		
}
void ItemsDialog::id_OkBtn_clicked(wxCommandEvent &event)
{
	this->EndModal(items_ok);
	this->Hide();
}
void ItemsDialog::id_CanBtn_clicked(wxCommandEvent &event)
{
	this->EndModal(items_can);
	this->Hide();
}
void ItemsDialog::id_AddItems_clicked(wxCommandEvent &event)
{
	if (id_InputItemsName->IsEmpty()) {
		wxMessageBox(_("You don't to input the Items name."),APPTITLE,
			wxOK|wxICON_EXCLAMATION,this);
		return;
	}
	DB_items db;
	db.SetFlag(add_mt);
	db.SetId(maxitemsId);
	maxitemsId++;
	db.SetImage(id_ItemsImage->GetSelection()-1);
	db.SetExpense(expenseId);
	db.SetName(id_InputItemsName->GetValue());
	dbitems->push_back(db);
	dbitemsId.push_back(db.GetId());
	id_ItemsList->Append(id_InputItemsName->GetValue());
	ClearControl();
}
void ItemsDialog::id_ModItems_clicked(wxCommandEvent &event)
{
	if (id_InputItemsName->IsEmpty()) {
		wxMessageBox(_("You don't to input the Items name."),APPTITLE,
			wxOK|wxICON_EXCLAMATION,this);
		return;
	}
	int sel = id_ItemsList->GetSelection();
	int oid = 0;
	for (int i = 0; i < (int)dbitems->size(); i++) {
		if ((dbitems->at(i).GetId() == dbitemsId.at(sel)) &&
			(dbitems->at(i).GetExpense() == expenseId)) {
			oid = i;
			break;
		}
	}
	if (dbitems->at(oid).GetFlag() != add_mt) {
		dbitems->at(oid).SetFlag(mod_mt);
	}
	//db.SetId(dbitems.size());

	dbitems->at(oid).SetImage(id_ItemsImage->GetSelection()-1);
	dbitems->at(oid).SetName(id_InputItemsName->GetValue());
	id_ItemsList->SetString(sel,id_InputItemsName->GetValue());
}
void ItemsDialog::id_DelItems_clicked(wxCommandEvent &event)
{
	int sel = id_ItemsList->GetSelection();
	if (dbitems->at(sel).GetFlag() == add_mt) {
		dbitems->erase(dbitems->begin()+sel);
	}else{
		int oid = 0;
		for (int i = 0; i < (int)dbitems->size(); i++) {
			if ((dbitems->at(i).GetId() == dbitemsId.at(sel)) &&
				(dbitems->at(i).GetExpense() == expenseId)) {
				oid = i;
				break;
			}
		}
		dbitems->at(oid).SetFlag(del_mt);
	}
	id_ItemsList->Delete(sel);
	ClearControl();
}
void ItemsDialog::id_ItemsList_selected(wxCommandEvent &event)
{
	int sel = event.GetSelection();
	if (sel >= 0) {
		for (int i = 0; i < (int)dbitems->size(); i++) {
			if ((dbitems->at(i).GetId() == dbitemsId.at(sel)) &&
				(dbitems->at(i).GetExpense() == expenseId)) {
				id_InputItemsName->SetValue(dbitems->at(i).GetName());
				id_ItemsImage->SetSelection(dbitems->at(i).GetImage()+1);
			}
		}
	}
}
void ItemsDialog::BindData_Items(int expense, std::vector<DB_items> *dbs, int maxcnt)
{
	expenseId = expense;
	dbitems = dbs;
	dbitemsId.clear();
	//dbitems.clear();
	//dbitems.assign(dbs.begin(), dbs.end());
	id_ItemsList->Clear();
	for (int i = 0; i < (int)dbitems->size(); i++) {
		if (dbitems->at(i).GetExpense() == expenseId) {
			id_ItemsList->Append(dbitems->at(i).GetName());
			dbitemsId.push_back(dbitems->at(i).GetId());
		}
	}
	maxitemsId = maxcnt;
}
std::vector<DB_items> ItemsDialog::GetChangedData(modifyType mt)
{
	std::vector<DB_items> result;
	int i = 0;
	for (i = 0; i < (int)dbitems->size(); i++) {
		if ((mt == none_mt) && (dbitems->at(i).GetFlag() != mt)) {
			result.push_back(dbitems->at(i));
		}else{
			if (dbitems->at(i).GetFlag() == mt) {
				result.push_back(dbitems->at(i));
			}
		}
	}
	return result;

}
int ItemsDialog::GetUpdatedItemsId(void)
{
	return maxitemsId;
}
void ItemsDialog::BindData_Icon()
{
	id_ItemsImage->Clear();
	id_ItemsImage->Append(_T("None"));
	for (int i = 0; i < (int)(wxGetApp().icoDList->GetImageCount()); i++) {
		id_ItemsImage->Append(StrF(_T("Icon %d"),i+1),wxGetApp().icoDList->GetBitmap(i));
	}
	id_ItemsImage->SetSelection(0);
}
